﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System.Windows.Threading;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Prompt;
using System.Text.RegularExpressions;


namespace Baosight.ZNH.ZMCB.UI.PopWindows
{
    public partial class S43AddYearPlanZB : EFViewCmes
    {

        public S43AddYearPlanZB(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“关闭”时执行此方法，重新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            //_view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                //case "insert":
                //    base.Mode = OperationMode.Insert;
                //    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                  //  BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            IList<EIRow> _list = new List<EIRow>();
            _list = this.grid.EIBlock.GetRows();
            EIRow eiRow = new EIRow();
            int a= this.grid.EIBlock.GetRowCount();
            for (int b = 0; b < a; b++)
                {
                    eiRow = this.grid.EIBlock.GetRow(b);
                    if (Validate(eiRow))
                    {
                        return;
                    }
                }
            EIInfo info = new EIInfo();
            EIBlock eIBlock = info.AddBlock("result");
            eIBlock.SetColumns(this.grid.EIBlock.GetColumns());
            eIBlock.AddRows(_list);
            info.Set(EIConstants.ServiceNameKey, "ZMCB43");
            info.Set(EIConstants.MethodNameKey, "update");
            this.Submit(info, DoCommitCompleted);
        }

        public bool Validate(EIRow eiRow)
        {
            string[] Column = new string[2] { "fdSurplusLength", "fdSurplusReserve"};
            for (int i = 0; i < 2; i++)
            {
                if (!(eiRow[Column[i]] == null))
                {

                    if (Regex.IsMatch(eiRow[Column[i]].ToString(), @"((^\d{1,8}$)|(^\d{1,8}\.\d{0,4}$)|(^0\.\d{0,4}$))"))
                    {
                        this.TempButtonOpen(true);//确定按钮生效
                        return false;

                    }
                    else
                    {
                        ValidateMessagePopup("输入的值只能输入整数位不超过8位，小数位不超过4位的数字");
                        this.TempButtonOpen(true);//确定按钮生效
                        return true;
                    }
                }
            }

            return false;
        }
        /// <summary>
        /// 提示错误消息
        /// </summary>
        /// <param name="str"></param>
        private void ValidateMessagePopup(string str)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(str);
            pop.Show(6);

            return;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }
    }
}

